﻿// var authority = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1]; //用户权限列表
//0: 查看项目 1: 管理项目 2: 查看委托单和委托人 3: 查看、新建（自己的）委托单 4: 新建、查看委托单和委托人
//5: 查看报告 6: 发布报告 7: 制定报告 8: 审核报告 9: 查看通知单 10: 发布通知单（评片） 11: 新建、匹配工艺卡
//12: 审核工艺卡 13: 分配检测小组 14: 分配检测人员 15: 查看派工单 16: 管理检测小组 17: 填写操作记录
//18: 分配权限 19: 检测部门管理
if ((authority[11] | authority[12]) === 0) {//检测主管审核工艺卡、检测主管新建 、匹配工艺卡
    alert("你没有权限查看此内容");
    window.location.href = "index.html";
}
var rawData = [[], [], []];
var title;
var methodCount;
var neededOptionIndex;
//显示选项卡

var optionCard = document.getElementById("optionCard");
var requestType;
//初始化表格标题，根据权限类型动态展示新建按钮和选项卡
if (authority[18] == 1){
    requestType = 'superManager'
}else{
    if(authority[11] === 1){
        requestType = 'manager'
    }
    if(authority[12] === 1){
        requestType = 'check'
    }
}
if (authority[11] === 1) {//检测主管新建、匹配工艺卡
    optionCard.innerHTML = "<li class=\"active\"><a href=\"#lC\" data-toggle=\"tab\">全部</a></li>" +
        "<li><a href=\"#lA\" data-toggle=\"tab\">审核中</a></li>" +
        "<li><a href=\"#lB\" data-toggle=\"tab\">已审核</a></li>";
    title = [
        ["编号", "项目名称", "被检产品名称", "工艺卡类型", "创建时间", "工艺卡状态","审核通过时间", "操作"],
        ["编号", "项目名称", "被检产品名称", "工艺卡类型", "创建时间", "工艺卡状态", "审核通过时间", "操作"],
        ["编号", "项目名称", "被检产品名称", "工艺卡类型", "创建时间", "工艺卡状态","审核通过时间",  "操作"]];
    methodCount = [7, 7, 7];
    neededOptionIndex = [
        [1, 2, 3, 5],
        [1, 2, 3, 5],
        [1, 2, 3, 5]
    ];
}
else if (authority[12] === 1) {//审核人员审核工艺卡
    optionCard.innerHTML = "<li class=\"active\"><a href=\"#lC\" data-toggle=\"tab\">全部</a></li>" +
        "<li><a href=\"#lA\" data-toggle=\"tab\">未审核</a></li>" +
        "<li><a href=\"#lB\" data-toggle=\"tab\">已审核</a></li>";
    title = [
        ["编号", "项目名称", "被检产品名称", "工艺卡类型", "创建时间", "工艺卡状态","审核通过时间", "操作"],
        ["编号", "项目名称", "被检产品名称", "工艺卡类型", "创建时间", "工艺卡状态", "审核通过时间", "操作"],
        ["编号", "项目名称", "被检产品名称", "工艺卡类型", "创建时间", "工艺卡状态","审核通过时间",  "操作"]];
    methodCount = [7, 7, 7];
    neededOptionIndex = [
        [1, 2, 3, 5],
        [1, 2, 3, 5],
        [1, 2, 3, 5]
    ];
}
///获取工艺卡列表.
let postData;
$.ajax({
    url: '/task/tecCard_list',
    type: 'post',
    data: { "requestType": requestType, "user_part": user.staff_part },
    async: false,
    success: function (data, status) {
        if (status == 'success') {
            postData = data;
            console.log(data)
            // window.location.href = '/projectManage.html';
        }

    },
    error: function (data, status) {
        if (status == 'error') {
            alert("你的网络有问题！");
        }
    }
});
for (let i = 0; i < postData.length; i++) {
    let state;
    switch (postData[i].tech_state) {//显示工艺卡的状态
        case 0: {
            state = "未审核";
            break;
        }
        case 1: {
            state = "审核通过";
            break;
        }
        case 2: {
            state = "审核不通过";
            break;
        }
    }
    // // 应该在tech_date 不为空时才能这样做
    // var date = new Date(postData[i].tech_date);
    // postData[i].tech_date = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
    // date.getDate();
    // //console.log(postData[i].tech_type);
    // var date1 = new Date(postData[i].tech_verify_date);
    // postData[i].tech_verify_date = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate();
    // date1.getDate();
    // 在tech_date不为空得时候才需要，对不符合日期格式得数据进行处理
    if(postData[i].tech_date != null){
        var date = new Date(postData[i].tech_date);
        postData[i].tech_date = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
        date.getDate();
    }
    if (postData[i].tech_verify_date!= null) {
        var date = new Date(postData[i].tech_verify_date);
        postData[i].tech_verify_date = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
        date.getDate();
    }
    let temp = [
        postData[i].tech_id,
        postData[i].task_pname,
        postData[i].task_product,
        postData[i].task_type ,
        postData[i].tech_date == null ? "-" : postData[i].tech_date,
        state,
        postData[i].tech_verify_date == null ? "-" : postData[i].tech_verify_date
    ];
    rawData[2].push(temp);
    switch (postData[i].tech_state) {
        case 0: {
            rawData[0].push(temp);
            break;
        }
        case 1:

        case 2: {
            rawData[1].push(temp);
        }
    }
}

var dummyData = [].concat(rawData);

var pageID = [1, 1, 1];
//设置每一页的行数
var countPerPage=[];
//默认情况下
if(user.countPerPage== 0){
    countPerPage = [10, 10, 10];
    $('.rowNum').val(10)
}else {
    countPerPage = [user.countPerPage,user.countPerPage,user.countPerPage];
    $('.rowNum').val(user.countPerPage)
}

//点击设置每一页行数的确定按钮
$('.rowsBut').click(function () {
    //获得文本框中输入的值
    var rowsNumbuer = $('.rowNum').val();
    //将该值传入到后台中
    $.ajax({
        url:'/pj/countPerPage',
        type:'post',
        data:{'rowsNumbuer': rowsNumbuer},
        async:false,
        success:function () {
            //console.log('点击确认以后的countPerPage'+user.userId)
            if(user.countPerPage== 0){
                countPerPage = [10, 10, 10];
                $('.rowNum').val(10)
            }else {
                countPerPage = [user.countPerPage,user.countPerPage,user.countPerPage];
                $('.rowNum').val(user.countPerPage)
            }
        },
        error:function () {
            if (status == 'error') {
                alert("你的网络有问题！");
            }
        }
    });
    //刷新页面
    location.reload()
})
var pageButtonCount = [8, 8, 8];
var pageCount = [parseInt((dummyData[0].length - 1) / countPerPage[0]) + 1,
parseInt((dummyData[1].length - 1) / countPerPage[1]) + 1,
parseInt((dummyData[2].length - 1) / countPerPage[2]) + 1];
var table = [document.getElementById("table0"), document.getElementById("table1"), document.getElementById("table2")];
var tbody = [table[0].children[1], table[1].children[1], table[2].children[1]];
var pages = [document.getElementById("pages0"), document.getElementById("pages1"), document.getElementById("pages2")];
console.log(tbody[0].innerHTML);
//初始化筛选栏可选项
for (let i = 0; i < neededOptionIndex.length; i++) {
    for (let j = 0; j < neededOptionIndex[i].length; j++) {
        let option = document.createElement("option");
        option.innerHTML = title[i][neededOptionIndex[i][j]];
        document.getElementById("searcher" + i).children[1].appendChild(option);
    }
}
//for (let i = 0; i < 3; i++) {
//    for (let j = 0; j < methodCount[i]; j++) {
//        let option = document.createElement("option");
//        option.innerHTML = title[i][j];
//        document.getElementById("searcher" + i).children[1].appendChild(option);
//    }
//}
//初始化表格
for (let i = 0; i < 3; i++) {
    let inner = "<td>&nbsp;</td>";
    for (let j = 0; j < title[i].length - 1; j++) {
        inner += "<td />";
    }
    for (let j = 0; j < countPerPage[i]; j++) {
        var tr = document.createElement("tr");
        tr.innerHTML = inner;
        tbody[i].appendChild(tr);
    }
}
//装填标题栏
for (let i = 0; i < 3; i++) {
    for (let j = 0; j < (pageCount[i] > pageButtonCount[i] ? pageButtonCount[i] : pageCount[i]); j++) {
        var li = document.createElement("li");
        li.setAttribute("onclick", "OnClick(" + (j + 1) + ", " + i + ")");
        var a = document.createElement("a");
        a.innerHTML = j + 1;
        a.setAttribute("href", "#");
        li.appendChild(a);
        pages[i].insertBefore(li, pages[i].lastElementChild);
    }
    for (let j = 0; j < title[i].length; j++) {
        let t = document.createElement("th");
        t.innerHTML = title[i][j];
        table[i].children[0].children[0].appendChild(t);
    }
    OnClick(1, i);
}
// 生成翻页按钮
// console.log(pages[0].innerHTML);
function OnClick(index, cardIndex) {
    // console.log(cardIndex);
    // console.log("a" + pageID);
    if (index === -1) {
        if (pageID[cardIndex] > 1) OnClick(pageID[cardIndex] - 1, cardIndex);
        return;
    }
    if (index === -2) {
        // console.log("b" + pageID);
        if (pageID[cardIndex] < pageCount[cardIndex]) OnClick(pageID[cardIndex] + 1, cardIndex);
        return;
    }
    if (index === 1 && dummyData[cardIndex].length < countPerPage[cardIndex]) {
        for (let i = 0; i < dummyData[cardIndex].length; i++) {
            tbody[cardIndex].children[i].setAttribute("style", "");
        }
        for (let i = dummyData[cardIndex].length; i < countPerPage[cardIndex]; i++) {
            tbody[cardIndex].children[i].setAttribute("style", "display: none");
        }
        pages[cardIndex].setAttribute("style", "display: none");
    }
    else {
        for (let i = 0; i < countPerPage[cardIndex]; i++) {
            tbody[cardIndex].children[i].setAttribute("style", "");
        }
        pages[cardIndex].setAttribute("style", "");
    }
    pageID[cardIndex] = index;
    // console.log("c" + pageID);
    //显示数据
    for (let i = 0; i < countPerPage[cardIndex]; i++) {
        if (dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i] !== undefined) {
            for (let j = 0; j < methodCount[cardIndex]; j++) {
                tbody[cardIndex].children[i].children[j].innerHTML = dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][j];
            }
            console.log('当前authority[12] = ', authority[12])
            if (authority[12] === 1) {//审核人员审核工艺卡
                let button = document.createElement("a");
                button.setAttribute("href", "javascript:void(0)");
                if (tbody[cardIndex].children[i].children[title[cardIndex].length - 3].innerHTML === "未审核") {
                    button.setAttribute("onclick", "window.location.href='/checkTecCard.html?id="
                        + dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][0] + "'");//pao
                    button.innerHTML = "审核";
                } else {
                    button.setAttribute("onclick", "window.location.href='/viewTecCard.html?id="
                        + dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][0] + "'");//pao
                    button.innerHTML = "查看";
                }
                tbody[cardIndex].children[i].children[title[cardIndex].length - 1].innerHTML = button.outerHTML;
            }
            if (authority[11] === 1) {//检测主管查看/修改工艺卡
                let button = document.createElement("a");
                button.setAttribute("href", "javascript:void(0)");
                if (tbody[cardIndex].children[i].children[title[cardIndex].length - 3].innerHTML === "审核不通过") {

                    button.setAttribute("onclick", "window.location.href='/modifyTecCard.html?id="
                        + rawData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][0] + "'");//pao
                    button.innerHTML = "修改";
                } else {
                    button.setAttribute("onclick", "window.location.href='/viewTecCard.html?id="
                        + dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][0] + "'");//pao
                    button.innerHTML = "查看";
                }
                tbody[cardIndex].children[i].children[title[cardIndex].length - 1].innerHTML = button.outerHTML;
            }
        } else {
            tbody[cardIndex].children[i].children[0].innerHTML = "&nbsp;";
           for (let j = 1; j < title[cardIndex].length; j++) {
               tbody[cardIndex].children[i].children[j].innerHTML = "";
            }

        }
    }
    //刷新翻页按钮
    if (pageCount[cardIndex] > pageButtonCount[cardIndex]) {
        var first = parseInt(pageID[cardIndex] - pageButtonCount[cardIndex] / 2) + 1;
        if (first < 1) first = 1;
        if (first > pageCount[cardIndex] - pageButtonCount[cardIndex] + 1) first = pageCount[cardIndex] - pageButtonCount[cardIndex] + 1;
        for (let i = 0; i < pageButtonCount[cardIndex]; i++) {
            pages[cardIndex].removeChild(pages[cardIndex].children[1]);
        }
        for (let i = first; i < first + pageButtonCount[cardIndex]; i++) {
            var li = document.createElement("li");
            li.setAttribute("onclick", "OnClick(" + i + ", " + cardIndex + ")");
            if (i === pageID[cardIndex])
                li.setAttribute("class", "active");
            var a = document.createElement("a");
            a.innerHTML = i;
            a.setAttribute("href", "#");
            li.appendChild(a);
            pages[cardIndex].insertBefore(li, pages[cardIndex].lastElementChild);
        }
    } else {
        for (let i = 0; i < pageCount[cardIndex]; i++) {
            pages[cardIndex].removeChild(pages[cardIndex].children[1]);
        }
        for (let i = 0; i < pageCount[cardIndex]; i++) {
            li = document.createElement("li");
            li.setAttribute("onclick", "OnClick(" + (i + 1) + ", " + cardIndex + ")");
            if (i + 1 === pageID[cardIndex])
                li.setAttribute("class", "active");
            a = document.createElement("a");
            a.innerHTML = i + 1;
            a.setAttribute("href", "#");
            li.appendChild(a);
            pages[cardIndex].insertBefore(li, pages[cardIndex].lastElementChild);
        }
    }
    // console.log("d" + pageID);
}

function OnConfirmDepute() {
    window.location.href = "newDepute.html?id=" + projectName[projectOption.selectedIndex][1];
}

function OnChange(select, index) {
    switch (select.selectedIndex) {
        case 1: {
            window.location.href = "/viewDepute.html?id=" + index;
            break;
        }
        case 2: {
            window.location.href = "/viewTechCard.html?id=" + index;
            break;
        }
        case 3: {
            window.location.href = "/viewWorkOrder.html?id=" + index;
            break;
        }
    }
}

var groupInfo = [["Group1", 1001], ["Group2", 1002]];
var deputeIndex;
function OnConfirmChooseGroup(index) {
    deputeIndex = index;
    document.getElementById("confirmChooseGroup").setAttribute("class", "modal fade in");
    document.getElementById("confirmChooseGroup").setAttribute("style", "display: block; padding-right: 17px;");
}

function OnChooseGroup(index) {
    deputeIndex = index;
    ChooseGroup();
}

function ChooseGroup() {
    document.getElementById("confirmChooseGroup").setAttribute("class", "modal fade");
    document.getElementById("confirmChooseGroup").setAttribute("style", "display: none;");
    let inner = "";
    for (let i = 0; i < groupInfo.length; i++) {
        inner += "<option>" + groupInfo[i][0] + "</option>";
    }
    document.getElementById("groupOption").innerHTML = inner;
    document.getElementById("chooseGroup").setAttribute("class", "modal fade in");
    document.getElementById("chooseGroup").setAttribute("style", "display: block; padding-right: 17px;");
}

function OnCloseGroup(id) {
    document.getElementById(id).setAttribute("class", "modal fade");
    document.getElementById(id).setAttribute("style", "display: none;");
}

function OnConfirmGroup() {
    let index = document.getElementById("groupOption").selectedIndex;
    alert("你选择了小组" + groupInfo[index][0] + "(" + groupInfo[index][1] + ")，保存功能未实现OvO");
    document.getElementById("chooseGroup").setAttribute("class", "modal fade");
    document.getElementById("chooseGroup").setAttribute("style", "display: none;");
    location.reload();
}

function OnSearch(cardIndex) {
    let searcher = document.getElementById("searcher" + cardIndex);//搜索栏对象
    let condition = searcher.children[1].selectedIndex;//下拉菜单中选择的那一项的下标
    let details = searcher.children[3].value;//检索的内容
    let timeRange = [searcher.children[5].value, searcher.children[7].value];//两个用来检索的时间，中间必定有一个逗号
    var handyData = rawData[cardIndex];//该页面上的信息
    var resultData = new Array();//存放检索结果
    for (let i = 0; i < handyData.length; i++) {
        //字符串匹配，长度为0则全匹配
        var regx = new RegExp(details);
        //if (regx.exec(handyData[i][condition])) {
        if (regx.exec(handyData[i][neededOptionIndex[cardIndex][condition]])) {
            if (searcher.children[5].value.length !== 0 && searcher.children[5].value > handyData[i][4])
                continue;
            if (searcher.children[7].value.length !== 0 && searcher.children[7].value < handyData[i][4])
                continue;
            resultData.push(handyData[i]);//加入符合的项
        }
    }
    dummyData[cardIndex] = resultData;//替换，反正再返回当前页面会变回来

    OnClick(1, cardIndex);
}
console.log(authority)